Motion in 2D¶
import numpy as np
%matplotlib inline
import matplotlib.pyplot as plt
from matplotlib import animation, rc
from IPython.display import HTML
Tutorial Problem 2.1, 2.2¶
Projectile motion
def get_xy(a, v0, start, end, interval):
angle = a * 2 * np.pi / 360
t = np.arange(start, end, interval)
# x(t) = vcos(angle)*t
x = v0 * np.cos(angle) * t
# y(t) = vsin(angle)*t - 1/2 gt**2
y = v0 * np.sin(angle) * t - 0.5 * 9.81 * t**2
return x, y
X1, Y1 = get_xy(53.1, 37, 0, 6.04, 0.01)
X2, Y2 = get_xy(89, 200, 0, 40.8, 0.1)
nframes = len(X1)
fig, ax = plt.subplots(figsize=(13,5))
ax.set_xlim((0, 135))
ax.set_ylim((0, 45))
ax.set_xlabel('x (m)')
ax.set_ylabel('y (m)')
ax.set_title('Projectile motion for tutorial problem 2.1 in real time')
line, = ax.plot([], [], 'ro', lw=2)
def init():
line.set_data([], [])
return (line,)
def animate(i):
x = X1[i]
y = Y1[i]
line.set_data(x, y)
return (line,)
anim = animation.FuncAnimation(fig, animate, init_func=init,
frames=nframes, interval=10,
blit=True)
HTML(anim.to_jshtml())